package 程序.文件上传;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class UploadClient {
    //定义要发送的文件的路径
    public static final String fileDir="D:\\Learn\\javademo\\untitled\\src\\程序\\文件上传";
    public static void main(String[] args) throws IOException {
        String fileName="\\read.txt";
        String filePath=fileDir+fileName;
        System.out.println("正在发送文件:"+filePath);
        Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9191);
        if(socket!=null){
            System.out.println("发送成功");
            SendFile(socket,filePath);
        }
    }
    private static void SendFile(Socket socket,String filePath) throws IOException {
        byte[] buffer=new byte[1024];
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File(filePath)));
        DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        //首先发送文件名，客户端使用writeUTF()方法，服务器端使用read UTF()方法
        dos.writeUTF(filePath);
        int len=0;
        while((len=bis.read(buffer,0,buffer.length))>0){
            dos.write(buffer,0,len);
            dos.flush();
        }
        dos.close();
        bis.close();
        socket.close();

    }
}
